package com.machine.filling.function.main.topFg;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.function.main.base.BaseFgFragment;
import com.machine.filling.function.main.cardInfoFg.CardInfoFgFragment;
import com.machine.filling.function.main.cardWarningFg.CardWarningFgFragment;
import com.machine.filling.function.main.orderInfoFg.OrderInfoFgFragment;
import com.machine.filling.net.ws.response.WsResTopGunsInfoBean;

/**
 * Created by Administrator on 2018/1/31 0031.
 * 进入这个界面就，开始获取油枪信息接口；  然后开始循环获取（每隔1分钟）；
 */

public class TopFgFragment extends BaseFgFragment<TopFgContract.Presenter> implements TopFgContract.View, View.OnClickListener {
    private static final String TAG = "TopFgFragment";

    private Button bt_card;
    private Button bt_order;

    private WsResTopGunsInfoBean.OilGunOutputBean gunInfoBean;
    private TextView tv_oil_type;
    private TextView tv_oil_gun;
    private TextView tv_price;

    public TopFgFragment() {
    }

    public void setDatas(WsResTopGunsInfoBean.OilGunOutputBean gunInfoBean) {
        this.gunInfoBean = gunInfoBean;
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_top, null);
        bt_card = (Button) view.findViewById(R.id.bt_card);
        bt_order = (Button) view.findViewById(R.id.bt_order);
        tv_oil_type = (TextView) view.findViewById(R.id.tv_oil_type);
        tv_oil_gun = (TextView) view.findViewById(R.id.tv_oil_gun);
        tv_price = (TextView) view.findViewById(R.id.tv_price);

        bt_card.setOnClickListener(this);
        bt_order.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        tv_oil_type.setText(gunInfoBean.getTypeName());
        tv_oil_gun.setText(gunInfoBean.getGunNum());
        tv_price.setText(gunInfoBean.getCuPrice()+"元/升");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_card://油卡加油
                Log.d(TAG, "onClick() returned: " + "点击油卡加油");
                Intent intent = new Intent();
                intent.putExtra("gunId", gunInfoBean.getGunId());
                if (cardInListener.isCardIn()) {
                    INextFragment.replaceNext(intent, CardInfoFgFragment.class.getName());
                }else {
                    //未插卡
                    INextFragment.replaceNext(intent, CardWarningFgFragment.class.getName());
                }
                break;
            case R.id.bt_order://预约加油
                Log.d(TAG, "onClick() returned: " + "点击预约加油");
                INextFragment.replaceNext(new Intent(), OrderInfoFgFragment.class.getName());
                break;
        }
    }

    private CardInListener cardInListener;

    /**
     * 设置油卡插入的监听
     * @param cardInListener
     */
    public void setCardInListener(CardInListener cardInListener) {
        this.cardInListener = cardInListener;
    }

    public interface CardInListener{
        boolean isCardIn();
    }

    /**
     * 更新ui
     * @param gunInfoBean
     */
    public void updataeUi(WsResTopGunsInfoBean.OilGunOutputBean gunInfoBean){
        tv_oil_type.setText(gunInfoBean.getTypeName());
        tv_oil_gun.setText(gunInfoBean.getGunName());
        tv_price.setText(gunInfoBean.getCuPrice()+"元/升");
    }
}
